// 项目相关类型定义
import { ApiResponse, PaginatedResponse } from './api'

// 项目状态类型 - 从状态管理系统动态获取
export type ProjectStatus = string

// 项目优先级枚举 - 与后端保持一致
export type ProjectPriority = 'low' | 'medium' | 'high' | 'urgent'

// 项目分类枚举 - 与后端保持一致
export type ProjectCategory = 
  | 'web_development'
  | 'mobile_app'
  | 'desktop_app'
  | 'api_development'
  | 'data_analysis'
  | 'machine_learning'
  | 'infrastructure'
  | 'testing'
  | 'maintenance'
  | 'research'
  | 'other'

// 项目成员角色枚举 - 与后端保持一致
export type ProjectMemberRole = 'owner' | 'manager' | 'developer' | 'tester' | 'designer' | 'analyst' | 'viewer'

// 项目成员接口 - 与后端API返回结构匹配
export interface ProjectMember {
  user_id: string
  role?: ProjectMemberRole // 保留用于向后兼容
  roles?: string[] // 项目角色ID列表（新字段）
  joined_at: string
  permissions: string[]
  // 用户信息（从后端返回时可能包含）
  name?: string
  user_name?: string
  username?: string
  email?: string
  user_email?: string
  avatar?: string
}

// 项目主接口 - 与后端API返回结构严格匹配
export interface Project {
  id: string
  name: string
  description?: string
  category: ProjectCategory
  priority: string
  status: ProjectStatus
  start_date?: string
  end_date?: string
  estimated_hours?: number
  budget?: number
  tags: string[]
  is_public: boolean
  organization_id?: string
  members: ProjectMember[]
  created_by: string
  created_by_name?: string  // 创建人名称
  created_at: string
  updated_at: string
  is_archived: boolean
  
  // 负责人字段
  owner_id?: string
  
  // 统计信息 - 与后端计算字段匹配
  task_count?: number
  completed_task_count?: number
  requirement_count?: number
  requirement_completed_count?: number
  test_case_count?: number
  defect_count?: number
  member_count?: number
  progress?: number
  
  // 置顶状态
  is_pinned?: boolean
}

// 项目创建表单 - 与后端ProjectCreate模型匹配
export interface ProjectCreateForm {
  name: string
  description?: string
  category: ProjectCategory
  priority: string
  status?: ProjectStatus
  start_date?: string
  end_date?: string
  estimated_hours?: number
  budget?: number
  tags?: string[]
  is_public: boolean
  organization_id?: string
  members?: ProjectMember[]
  
  // 负责人字段
  owner_id?: string
}

// 项目更新表单 - 与后端ProjectUpdate模型匹配
export interface ProjectUpdateForm {
  name?: string
  description?: string
  category?: ProjectCategory
  priority: string
  status?: ProjectStatus
  start_date?: string
  end_date?: string
  estimated_hours?: number
  budget?: number
  tags?: string[]
  is_public?: boolean
  
  // 负责人字段
  project_manager?: string
}

// 项目统计信息
export interface ProjectStats {
  total_projects: number
  active_projects: number
  completed_projects: number
  projects_by_status: Record<string, number>
  projects_by_priority: Record<string, number>
  projects_by_category: Record<string, number>
}

// 项目活动记录
export interface ProjectActivity {
  id: string
  project_id: string
  user_id: string
  user_name: string
  action: string
  description: string
  metadata: Record<string, any>
  created_at: string
}

// 项目筛选条件
export interface ProjectFilter {
  search?: string
  status?: ProjectStatus
  priority: string
  category?: ProjectCategory
  created_by?: string
  member_id?: string
  organization_id?: string
  is_archived?: boolean
  start_date?: string
  end_date?: string
}

// 项目成员添加表单
export interface ProjectMemberAdd {
  user_id: string
  role: ProjectMemberRole
  permissions: string[]
}

// 项目成员更新表单
export interface ProjectMemberUpdate {
  role?: ProjectMemberRole
  permissions?: string[]
}

// 项目摘要信息
export interface ProjectSummary {
  id: string
  name: string
  status: ProjectStatus
  priority: string
  progress?: number
  member_count: number
  task_count: number
  created_at: string
}

// 项目分类选项 - 更新为与后端枚举匹配
export const PROJECT_CATEGORIES = [
  { value: 'web_development', label: 'Web开发' },
  { value: 'mobile_app', label: '移动应用' },
  { value: 'desktop_app', label: '桌面应用' },
  { value: 'api_development', label: 'API开发' },
  { value: 'data_analysis', label: '数据分析' },
  { value: 'machine_learning', label: '机器学习' },
  { value: 'infrastructure', label: '基础设施' },
  { value: 'testing', label: '测试' },
  { value: 'maintenance', label: '维护' },
  { value: 'research', label: '研究' },
  { value: 'other', label: '其他' }
] as const

// 项目优先级选项
export const PROJECT_PRIORITIES = [
  { value: 'low', label: '低', color: '#909399' },
  { value: 'medium', label: '中', color: '#E6A23C' },
  { value: 'high', label: '高', color: '#F56C6C' },
  { value: 'urgent', label: '紧急', color: '#F56C6C' }
] as const

// 项目状态选项 - 已迁移到 useProjectStates composable
// 使用动态状态管理，不再使用硬编码数组
// 保留此导出仅为向后兼容，建议使用 useProjectStates() 获取状态选项
export const PROJECT_STATUSES: Array<{ value: string; label: string; color: string }> = []

// 项目成员角色选项
export const PROJECT_MEMBER_ROLES = [
  { value: 'owner', label: '项目负责人' },
  { value: 'manager', label: '项目经理' },
  { value: 'developer', label: '开发人员' },
  { value: 'tester', label: '测试人员' },
  { value: 'designer', label: '设计师' },
  { value: 'analyst', label: '分析师' },
  { value: 'viewer', label: '查看者' }
] as const

// 项目权限选项 - 按模块分组
export const PROJECT_PERMISSIONS = [
  // 项目权限
  { value: 'project:read', label: '查看项目', module: 'project' },
  { value: 'project:update', label: '编辑项目', module: 'project' },
  { value: 'project:delete', label: '删除项目', module: 'project' },
  { value: 'project:manage_members', label: '管理成员', module: 'project' },
  
  // 需求权限
  { value: 'requirement:read', label: '查看需求', module: 'requirement' },
  { value: 'requirement:create', label: '创建需求', module: 'requirement' },
  { value: 'requirement:update', label: '编辑需求', module: 'requirement' },
  { value: 'requirement:delete', label: '删除需求', module: 'requirement' },
  
  // 任务权限
  { value: 'task:read', label: '查看任务', module: 'task' },
  { value: 'task:create', label: '创建任务', module: 'task' },
  { value: 'task:update', label: '编辑任务', module: 'task' },
  { value: 'task:delete', label: '删除任务', module: 'task' },
  { value: 'task:assign', label: '分配任务', module: 'task' },
  
  // 缺陷权限
  { value: 'defect:read', label: '查看缺陷', module: 'defect' },
  { value: 'defect:create', label: '创建缺陷', module: 'defect' },
  { value: 'defect:update', label: '编辑缺陷', module: 'defect' },
  { value: 'defect:delete', label: '删除缺陷', module: 'defect' },
  { value: 'defect:assign', label: '分配缺陷', module: 'defect' },
  
  // 测试权限
  { value: 'test_case:read', label: '查看测试用例', module: 'test_case' },
  { value: 'test_case:create', label: '创建测试用例', module: 'test_case' },
  { value: 'test_case:update', label: '编辑测试用例', module: 'test_case' },
  { value: 'test_case:delete', label: '删除测试用例', module: 'test_case' },
  { value: 'test_case:assign', label: '分配测试用例', module: 'test_case' },
  { value: 'test_case:execute', label: '执行测试用例', module: 'test_case' }
] as const

// 权限模块分组
export const PERMISSION_MODULES = {
  project: { label: '项目权限', icon: 'Folder' },
  requirement: { label: '需求权限', icon: 'Document' },
  task: { label: '任务权限', icon: 'List' },
  defect: { label: '缺陷权限', icon: 'Warning' },
  test_case: { label: '测试权限', icon: 'Check' }
} as const

// API响应类型定义 - 基于统一响应格式
export type ProjectListResponse = PaginatedResponse<Project>
export type ProjectDetailResponse = ApiResponse<Project>
export type ProjectStatsResponse = ApiResponse<ProjectStats>
export type ProjectCreateResponse = ApiResponse<Project>
export type ProjectUpdateResponse = ApiResponse<Project>
export type ProjectDeleteResponse = ApiResponse<null>